
//
//  LDBottomPopView.m
//  指乎
//
//  Created by Dean on 2018/11/20.
//  Copyright © 2018 Dean. All rights reserved.
//

#import "LDBottomPopView.h"
#define kCurrentWindow [APPDelegate window]

#define tBarHeright RatioHeight(48)
@interface LDBottomPopView ()

@property (nonatomic, weak) UIView *alertView;
@property (nonatomic, weak) UITapGestureRecognizer *singleTap;
@end
@implementation LDBottomPopView

- (instancetype)initWithAlertView:(UIView *)tipView
{
    if (self = [self initWithFrame:[APPDelegate window].bounds]) {
        [self addSubview:tipView];
    
        
        self.backgroundColor = RGBCOLOR(0, 0, 0, .4);
        _alertView = tipView;
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, self.width, _alertView.height + tBarHeright+(IS_IPHONEX?20:0))];
        [self addSubview:self.backView];
        self.backView.backgroundColor = [UIColor whiteColor];
        
        [self.backView addSubview:self.alertView];
        self.alertView.top = tBarHeright;
        _sureBtnEnable = YES;
        UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancel setTitle:@"取消" forState:0];
        cancel.frame = CGRectMake(0, 0, RatioWidth(66), tBarHeright);
        cancel.titleLabel.font = TGAutoFont(15);
        [cancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        [cancel setTitleColor:RGBA(153, 153, 153, 1) forState:0];
        _leftBtn = cancel;
        [self.backView addSubview:cancel];
        
        UIButton *sure = [UIButton buttonWithType:UIButtonTypeCustom];
        [sure setTitle:@"确定" forState:0];
        sure.frame = CGRectMake(self.width - RatioWidth(66), 0, RatioWidth(66), tBarHeright);
        _rightBtn = sure;
        [sure setTitleColor:LightGrayColor forState:UIControlStateDisabled];
        
        sure.titleLabel.font = TGAutoFont(15);
        [sure setTitleColor:RGBA(51, 51, 51, 1) forState:0];
        [self.backView addSubview:sure];
        [sure addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];

        UILabel *line =  [[UILabel alloc] initWithFrame:CGRectMake(0, 9, self.width,PixelOne)];
        line.backgroundColor = UIColorSeparator;
        line.top = tBarHeright - line.height;
        _lineView = line;
        [self.backView addSubview:line];
          self.backView.layer.cornerRadius = 6.;
//        self.backView.layer.shadowColor = BGGrayColor.CGColor;//shadowColor阴影颜色
//        self.backView.layer.shadowOffset = CGSizeMake(0,4);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
//        self.backView.layer.shadowOpacity = 1;//阴影透明度，默认0
//        self.backView.backgroundColor = [UIColor whiteColor];//[UIColor colorWithWhite:0.2 alpha:1];
        
        
    }
    return self;
}

- (instancetype)initWithContentView:(UIView *)contentView {
    if (self = [self initWithFrame:[APPDelegate window].bounds]) {
//        [self addSubview:contentView];
    
        self.backgroundColor = RGBCOLOR(0, 0, 0, .4);
        
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, self.width, contentView.height)];
        [self addSubview:self.backView];
        self.backView.backgroundColor = [UIColor whiteColor];
        
        [self.backView addSubview:contentView];
       
        self.backView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner|QMUILayerMaxXMinYCorner;
        self.backView.layer.cornerRadius = 12;
        self.backView.layer.masksToBounds = YES;
    }
    return self;
}

- (void)setCenterTitle:(NSString *)centerTitle {
    _centerTitle = centerTitle;
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = centerTitle;
    titleLabel.textColor = LightGrayColor;
    titleLabel.font = TGBlodAFont(15);
//    titleLabel.centerY = self.leftBtn.centerY;
//    titleLabel.centerX = self.centerX;
//    titleLabel.size = CGSizeMake(150, 35);
    [self.backView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self.leftBtn);
    }];
}

- (void)addTagert:(id)tagert selector:(SEL)sel {
    [_rightBtn removeTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [_rightBtn addTarget:tagert action:sel forControlEvents:UIControlEventTouchUpInside];

}
- (void)setSureBtnEnable:(BOOL)sureBtnEnable {
    _sureBtnEnable = sureBtnEnable;
    self.rightBtn.enabled = _sureBtnEnable;
}
- (void)cancelAction:(id)sender{
    
    [self hide];
}

- (void)sureAction:(id)sender{
    if (self.sureblock) {
        self.sureblock();
    }
    [self hide];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self addBackgroundView];
        [self addSingleGesture];
    }
    return self;
}
- (void)addBackgroundView
{
    if (_backgroundView == nil) {
        UIImageView *backgroundView = [[UIImageView alloc]initWithFrame:[APPDelegate window].bounds];
        backgroundView.userInteractionEnabled = YES;
        backgroundView.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        _backgroundView = backgroundView;
    }
    [self addSubview:_backgroundView];
    
}
#pragma mark - add Gesture
- (void)addSingleGesture
{
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    [_backgroundView addGestureRecognizer:singleTap];
    _singleTap = singleTap;
}
#pragma mark 手指点击事件
- (void)singleTap:(UITapGestureRecognizer *)sender
{
    [self hide];
}

- (void)show
{
    if (self.superview == nil) {
        [kCurrentWindow addSubview:self];
    }
    self.alpha = 0;
    self.backView.frame = CGRectMake(0, SCREEN_HEIGHT,self.backView.width, self.backView.height);
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.frame = CGRectMake(0, SCREEN_HEIGHT-self.backView.height,self.backView.width, self.backView.height);
        self.alpha = 1;
    }];
    
}

- (void)hide
{
    if (self.superview) {
        [UIView animateWithDuration:0.3 animations:^{
//            self.backgroundView.alpha = 1;
            self.backView.frame = CGRectMake(0, SCREEN_HEIGHT,self.backView.width, self.backView.height);
        } completion:^(BOOL finished) {
            if (self.hideblock) {
                self.hideblock();
            }
            [self removeFromSuperview];
        }];
    }
}
@end
